// 滑动窗口


function minSubArrayLen(target, nums) {
  let left = 0; // 窗口左边界
  let sum = 0;  // 窗口内元素的和
  let minLength = Infinity; // 初始化为最大值

  for (let right = 0; right < nums.length; right++) {
    sum += nums[right]; // 扩展窗口右边界

    // 当窗口内和满足条件时，尝试收缩左边界
    while (sum >= target) {
      minLength = Math.min(minLength, right - left + 1);
      sum -= nums[left]; // 收缩窗口左边界
      left++;
    }
  }

  return minLength === Infinity ? 0 : minLength;
}

// 使用示例
console.log(minSubArrayLen(7, [2, 3, 1, 2, 4, 3])); // 2 → [4, 3]
console.log(minSubArrayLen(4, [1, 4, 4]));         // 1 → [4]
console.log(minSubArrayLen(11, [1, 1, 1, 1, 1]));   // 0 → 无满足条件的子数组